<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索框 -->
    <el-row>
      <el-col :span="8">
        <el-input v-model="queryInfo.query" clearable @clear="clearSearch">
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="searchGoods"
          ></el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button class="search" type="primary" @click="addGoods">添加商品</el-button>
      </el-col>
    </el-row>
    <!-- 列表 -->
    <el-table :data="goodsList" style="width: 100%" border stripe>
      <el-table-column type="index"> </el-table-column>
      <el-table-column prop="goods_name" label="商品名称" width="400">
      </el-table-column>
      <el-table-column prop="goods_price" label="商品价格(元)">
      </el-table-column>
      <el-table-column prop="goods_weight" label="商品重量"> </el-table-column>
      <el-table-column prop="add_time" label="创建时间" width="200">
        <template slot-scope="scoped">{{
          scoped.row.add_time | dateFormat
        }}</template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scoped">
          <el-button type="primary" icon="el-icon-edit"></el-button>
          <el-button type="danger" icon="el-icon-delete" @click="del(scoped.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      background
    >
    </el-pagination>
  </div>
</template>
<script>
import {confirm} from '../../utils/index'
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      goodsList: [],
    };
  },
  methods: {
    //删除商品
    async del(row){
        let id=row.goods_id
        let confirmRes=await confirm().catch(err=>err)
        if(confirmRes!='confirm')return this.$message.info("已取消删除商品")
        let {data:res}=await this.$axios.delete('/goods'+'/'+id)
        if(res.meta.status!=200)return this.$message.error("删除失败")
        this.$message.success("删除成功")
        this.getGoodsList()
    },   
      addGoods(){
          this.$router.push('/addGoods')
      },
    clearSearch() {
      this.queryInfo = {
        query: "",
        pagenum: 1,
        pagesize: 5,
      };
      this.getGoodsList();
    },
    searchGoods() {
      this.queryInfo.pagenum = 1;
      this.getGoodsList();
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getGoodsList();
    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getGoodsList();
    },
    // 封装获得商品列表的函数
    async getGoodsList() {
      let { data: res } = await this.$axios.get("/goods", {
        params: this.queryInfo,
      });
      if (res.meta.status != 200) return this.$message.error("获取数据失败");
      this.total = res.data.total;
      this.goodsList = res.data.goods;
    },
  },
  created() {
    this.getGoodsList();
  },
};
</script>
<style lang="less" scoped>
.search {
  margin-left: 20px;
}
.el-table {
  text-align: center;
  margin-top: 20px;
}
</style>